/*
 *  Copyright (c) M2TK Project. All rights reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package m2tk.assistant.app.ui.component;

import m2tk.assistant.app.ui.model.ServiceInfoTableModel;
import m2tk.assistant.app.ui.util.ComponentUtil;
import m2tk.assistant.app.ui.util.ThreeStateRowSorterListener;
import m2tk.assistant.common.domain.SIService;
import m2tk.assistant.common.ui.RoundPanel;
import net.miginfocom.swing.MigLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableRowSorter;
import java.util.List;

public class ServiceInfoPane extends RoundPanel
{
    private static final Logger log = LoggerFactory.getLogger(ServiceInfoPane.class);

    private ServiceInfoTableModel modelActualTS;
    private ServiceInfoTableModel modelOtherTS;

    public ServiceInfoPane()
    {
        initUI();
    }

    private void initUI()
    {
        modelActualTS = new ServiceInfoTableModel();
        modelOtherTS = new ServiceInfoTableModel();

        TableRowSorter<ServiceInfoTableModel> rowSorterActualTS = new TableRowSorter<>(modelActualTS);
        rowSorterActualTS.addRowSorterListener(new ThreeStateRowSorterListener(rowSorterActualTS));
        TableRowSorter<ServiceInfoTableModel> rowSorterOtherTS = new TableRowSorter<>(modelOtherTS);
        rowSorterOtherTS.addRowSorterListener(new ThreeStateRowSorterListener(rowSorterOtherTS));

        JTable table1 = createTable();
        table1.setModel(modelActualTS);
        table1.setRowSorter(rowSorterActualTS);
        table1.getTableHeader().setReorderingAllowed(false);
        table1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        table1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        JTable table2 = createTable();
        table2.setModel(modelOtherTS);
        table2.setRowSorter(rowSorterOtherTS);
        table2.getTableHeader().setReorderingAllowed(false);
        table2.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        table2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        DefaultTableCellRenderer leadingRenderer = new DefaultTableCellRenderer();
        leadingRenderer.setHorizontalAlignment(SwingConstants.LEADING);

        TableColumnModel columnModel = table1.getColumnModel();
        ComponentUtil.configTableColumn(columnModel, 0, leadingRenderer, 80, false); // 序号
        ComponentUtil.configTableColumn(columnModel, 1, leadingRenderer, 200, true); // 业务名称
        ComponentUtil.configTableColumn(columnModel, 2, leadingRenderer, 100, true); // 提供商
        ComponentUtil.configTableColumn(columnModel, 3, leadingRenderer, 120, true); // 业务类型
        ComponentUtil.configTableColumn(columnModel, 4, leadingRenderer, 100, false); // 业务号
        ComponentUtil.configTableColumn(columnModel, 5, leadingRenderer, 100, false); // 传输流号
        ComponentUtil.configTableColumn(columnModel, 6, leadingRenderer, 100, false); // 原始网络号

        columnModel = table2.getColumnModel();
        ComponentUtil.configTableColumn(columnModel, 0, leadingRenderer, 80, false); // 序号
        ComponentUtil.configTableColumn(columnModel, 1, leadingRenderer, 200, true); // 业务名称
        ComponentUtil.configTableColumn(columnModel, 2, leadingRenderer, 100, true); // 提供商
        ComponentUtil.configTableColumn(columnModel, 3, leadingRenderer, 120, true); // 业务类型
        ComponentUtil.configTableColumn(columnModel, 4, leadingRenderer, 100, false); // 业务号
        ComponentUtil.configTableColumn(columnModel, 5, leadingRenderer, 100, false); // 传输流号
        ComponentUtil.configTableColumn(columnModel, 6, leadingRenderer, 100, false); // 原始网络号

        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.add("当前传输流", createScrollPane(table1));
        tabbedPane.add("其他传输流", createScrollPane(table2));

        setLayout(new MigLayout("insets 10", "[][grow][]", "[25!][pref!][][grow]"));
        add(new JLabel("DVB业务",
                       StreamInfoNaviPane.BOARD_ICON,
                       SwingConstants.TRAILING), "left, wrap");
        add(separator(), "span 3, grow, wrap");
        add(tabbedPane, "span 3 2, grow");
    }

    public void updateServices(List<SIService> srvActualTS, List<SIService> srvOtherTS)
    {
        modelActualTS.update(srvActualTS);
        modelOtherTS.update(srvOtherTS);
    }
}
